﻿using DevExpress.XtraCharts;
using MDIDemo.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DevMy.Vou
{
    public partial class Form_ChartDemo : DevExpress.XtraEditors.XtraForm
    {
        public Form_ChartDemo()
        {
            InitializeComponent();
        }
        private int Count = 6;
        private ValueModel[] GetSeriesData(double value)
        {
            int index = 20;
            ValueModel[] valueModels = new ValueModel[index];
            DateTime dateTime = DateTime.Now;
            for (int i = 0; i < index; i++)
            {
                valueModels[i] = new ValueModel(CalculateNextValue(value), dateTime.AddMinutes(i));
            }
            return valueModels;
        }
        private void SetChartControl(ChartControl chartControl)
        {
            chartControl.BorderOptions.Thickness = 10;
            chartControl.CrosshairEnabled = DevExpress.Utils.DefaultBoolean.True;
            chartControl.DataBindings = null;
            chartControl.Legend.AlignmentHorizontal = DevExpress.XtraCharts.LegendAlignmentHorizontal.Left;
            chartControl.Legend.AlignmentVertical = DevExpress.XtraCharts.LegendAlignmentVertical.TopOutside;
            chartControl.Legend.Direction = DevExpress.XtraCharts.LegendDirection.LeftToRight;
            chartControl.Legend.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            chartControl.Legend.Name = "Default Legend";
            chartControl.Legend.UseCheckBoxes = true;
            Series[] series = new Series[Count * 2];
            for (int i = 0; i < Count * 2; i++)
            {
                series[i] = SetSerise(i);
            }
            chartControl.SeriesSerializable = series;
            DevExpress.XtraCharts.ChartTitle chartTitle3 = new DevExpress.XtraCharts.ChartTitle();
            chartTitle3.Dock = DevExpress.XtraCharts.ChartTitleDockStyle.Bottom;
            chartTitle3.Text = "实时监测数据";
            chartControl.Titles.AddRange(new DevExpress.XtraCharts.ChartTitle[] {
            chartTitle3});
            SwiftPlotDiagram diagram = this.chartControl1.Diagram as SwiftPlotDiagram;
            diagram.AxisX.GridLines.Visible = true;
            diagram.AxisX.Label.TextPattern = "{A:hh:mm}";
            diagram.AxisX.Title.Text = "时间";
            diagram.AxisX.VisibleInPanesSerializable = "-1";
            diagram.AxisY.Interlaced = true;
            diagram.AxisY.Title.Text = "数值";
            diagram.AxisY.VisibleInPanesSerializable = "-1";
            diagram.AxisY.WholeRange.AlwaysShowZeroLevel = false;
            diagram.Margins.Right = 15;
        }
        private Series SetSerise(int count)
        {
            Series series = new Series();
            DevExpress.XtraCharts.SwiftPlotSeriesView swiftPlotSeriesView1 = new DevExpress.XtraCharts.SwiftPlotSeriesView();
            series.ArgumentScaleType = DevExpress.XtraCharts.ScaleType.DateTime;
            if (count < 6)
                series.Name = "波长" + (count + 1);
            else
                series.Name = "能量" + (count - 6 + 1);
            series.View = swiftPlotSeriesView1;
            return series;
        }
        private void Form_ChartDemo_Load(object sender, EventArgs e)
        {
            SetChartControl(this.chartControl1);
        }

        Random random = new Random();
        double CalculateNextValue(double iniValue)
        {
            return iniValue + random.NextDouble() * 10.0 - 5.0;
        }
        void AddPoints(DevExpress.XtraCharts.Series series, SeriesPoint[] pointsToUpdate)
        {
            if (series.View is SwiftPlotSeriesViewBase)
                series.Points.AddRange(pointsToUpdate);
        }

        private void CreateData1()
        {
            int Interval = 2;
            double value1 = 1500.0;
            double value2 = -24.0;
            int TimeInterval = 20;
            DateTime argument = DateTime.Now;
            SeriesPoint[,] seriesPoints = new SeriesPoint[Count * 2, Interval];

            for (int i = 0; i < Count * 2; i++)
            {
                SeriesPoint[] pointsToUpdate = new SeriesPoint[Interval];
                for (int k = 0; k < Interval; i++)
                {
                    if (Count < 6)
                    {
                        pointsToUpdate[k] = new SeriesPoint(argument, value1);
                        value1 = CalculateNextValue(value1);
                    }
                    else
                    {
                        pointsToUpdate[k] = new SeriesPoint(argument, value2);
                        value2 = CalculateNextValue(value2);
                    }
                    argument = argument.AddMilliseconds(1);
                }
                DateTime minDate = argument.AddSeconds(-TimeInterval);
                int pointsToRemoveCount = 0;
                Series series = this.chartControl1.Series[i];
                foreach (SeriesPoint point in series.Points)
                    if (point.DateTimeArgument < minDate)
                        pointsToRemoveCount++;
                if (pointsToRemoveCount < series.Points.Count)
                    pointsToRemoveCount--;
                AddPoints(this.chartControl1.Series[i], pointsToUpdate);
                SwiftPlotDiagram diagram = this.chartControl1.Diagram as SwiftPlotDiagram;
                if (diagram != null && (diagram.AxisX.DateTimeScaleOptions.MeasureUnit == DateTimeMeasureUnit.Millisecond
                    || diagram.AxisX.DateTimeScaleOptions.ScaleMode == ScaleMode.Continuous))
                    diagram.AxisX.WholeRange.SetMinMaxValues(minDate, argument);
            }
            //SeriesPoint[] pointsToUpdate1 = new SeriesPoint[Interval];
            //SeriesPoint[] pointsToUpdate2 = new SeriesPoint[Interval];
            //for (int i = 0; i < Interval; i++)
            //{
            //    pointsToUpdate1[i] = new SeriesPoint(argument, value1);
            //    pointsToUpdate2[i] = new SeriesPoint(argument, value2);
            //    argument = argument.AddMilliseconds(1);
            //    value1 = CalculateNextValue(value1);
            //    value2 = CalculateNextValue(value2);
            //}
            //DateTime minDate = argument.AddSeconds(-TimeInterval);
            //int pointsToRemoveCount = 0;
            //Series series = this.chartControl1.Series[0];
            //foreach (SeriesPoint point in series.Points)
            //    if (point.DateTimeArgument < minDate)
            //        pointsToRemoveCount++;
            //if (pointsToRemoveCount < series.Points.Count)
            //    pointsToRemoveCount--;



            //if (pointsToRemoveCount > 0)
            //{
            //    //this.chartControl1.Series[0].Points.RemoveRange(0, pointsToRemoveCount);
            //    //this.chartControl1.Series[1].Points.RemoveRange(0, pointsToRemoveCount);
            //}
            //SwiftPlotDiagram diagram = this.chartControl1.Diagram as SwiftPlotDiagram;
            //if (diagram != null && (diagram.AxisX.DateTimeScaleOptions.MeasureUnit == DateTimeMeasureUnit.Millisecond
            //    || diagram.AxisX.DateTimeScaleOptions.ScaleMode == ScaleMode.Continuous))
            //    diagram.AxisX.WholeRange.SetMinMaxValues(minDate, argument);
        }

        private void CreateData2()
        {
            foreach (Series item in this.chartControl1.Series)
            {
                item.Points.Clear();
            }
            for (int i = 0; i < Count * 2; i++)
            {
                ValueModel[] valueModels;
                Series series = this.chartControl1.Series[i];
                if (i < 6)
                    valueModels = GetSeriesData(1500.0);
                else
                    valueModels = GetSeriesData(-20.0);
                foreach (ValueModel item in valueModels)
                {
                    SeriesPoint seriesPoint = new SeriesPoint(item.dateTime.ToString("hh:mm:ss"), item.Value);
                    series.Points.Add(seriesPoint);
                }
            }
        }

        private void CreateData3()
        {
            foreach (Series item in this.chartControl1.Series)
            {
                if (item.Points.Count > 20)
                    item.Points.RemoveAt(0);
            }
            int index = 0;
            DateTime dateTime = DateTime.Now;
            double value = 0.0;
            foreach (Series item in this.chartControl1.Series)
            {
                if (index < 6)
                    value = 1500.00;
                else
                    value = -20.00;
                ValueModel valueModel = new ValueModel(CalculateNextValue(value), dateTime.AddMinutes(index++));
                SeriesPoint seriesPoint = new SeriesPoint(valueModel.dateTime.ToString("hh:mm:ss"), valueModel.Value);
                item.Points.Add(seriesPoint);
            }
        }
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            //CreateData2();
            CreateData3();
        }
    }
}
